<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Role extends Admincontroller
{
    public function list()
    {
        //获取用户表信息
        $list = Db::name('roles')->field(true)->order('id','ASC')->select();
        return $this->fetch('role/list',[
            'list' => $list
        ]);
    }

    public function add()
    {
        $data = $_POST;
        //var_dump($data);
        $result = Db::name('roles')->data($data)->insert();
        if ($result > 0) {
            return $this->success('添加成功', url('/adminRoleList'));
        } else {
            return $this->error('添加失败(ノ°ο°)ノ高能预警 ', url('/adminRoleList'));
        }
    }

    public function edit($id)
    {
        // 查询指定用户信息
        $row = Db::name('roles')->field('pass', true)->find($id);
        return view('role/edit', [
            'data' => $row
        ]);
    }

    public function doedit($id)
    {
        $data = $_POST;
        $result = Db::name('roles')->where('id',$id)->update($data);
        if ($result < 0) {
            return $this->error('添加失败(ノ°ο°)ノ高能预警 ');
        } else {
            return $this->success('添加成功', url('/adminRoleList'));
        }
    }

    public function editpower($id)
    {
        $list = Db::name('powers')->field(true)->order('id','ASC')->select();
        $result = Db::name('role_powers')->where('role_id',$id)->select();
        $power[0] = 0;
        foreach($result as $res)
        {
            //var_dump($res['power_id']);
            $power[] = $res['power_id'];
        }
        // var_dump($power);
        // exit;
        // var_dump($list);
        return $this->fetch('role/editpower',[
            'id' => $id,
            'list' => $list,
            'power' => $power
        ]);
    }

    public function doeditpower(Request $request)
    {
        $p=$request->post();
        $info = $p;
        $role_id = $p['role_id'];
        $power_id  =$p['power_id'];
        $res = Db::name('role_powers')->where('role_id',$role_id)->where('power_id',$power_id)->find();
        if($res){
            $result = Db::name('role_powers')->where('role_id',$role_id)->where('power_id',$power_id)->delete();
            $info['status'] = '1';
        }else{
            $data = [
            'role_id' => $p['role_id'],
            'power_id'  => $p['power_id']
        ];
            $result = Db::name('role_powers')->data($data)->insert();
            $info['stauts'] = '2';
        }
        return json($info);
    }

    public function delete($id)
    {
        $result = Db::name('roles')->delete($id);
        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的角色删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的角色删除失败!';
        }

        return json($info);
    }
}
